package org.jc.subject.domain.handler.subject;

import jakarta.annotation.Resource;
import org.jc.subject.common.enums.SubjectInfoTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述
 *
 * @author wanghongqian
 * @date 2024/06/24 23:24
 */
@Component
public class SubjectTypeHandlerFactory implements InitializingBean {

    @Resource
    private List<SubjectTypeHandler> subjectTypeHandlerList;

    private Map<SubjectInfoTypeEnum,SubjectTypeHandler> subjectTypeHandlerMap = new HashMap<>();

    public SubjectTypeHandler getSubjectTypeHandler(int subjectType){
        SubjectInfoTypeEnum subjectInfoTypeEnum = SubjectInfoTypeEnum.getByCode(subjectType);
        return subjectTypeHandlerMap.get(subjectInfoTypeEnum);
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        subjectTypeHandlerList.forEach(handler ->{
            subjectTypeHandlerMap.put(handler.getHandlerType(),handler);
        });
    }
}
